package recommenRatings.data;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import recommenRatings.domain.Message;
import recommenRatings.domain.Place;
import recommenRatings.domain.User;
import recommenRatings.utils.UIDGenerator;

public class JDBCMessageDAO implements IMessageDAO {

	public boolean insert(Message m) {
		Boolean b = false;
		Connection conn = ConnectionManager.getInstance().checkOut();

		String sql = "INSERT INTO Message VALUES (?, ?, ?, ?, ?, ?, ?) ";
		PreparedStatement stmt = null;
		
		try {
			
			Date msgDate = new Date(m.getDate().getTime());
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, m.getText());
			stmt.setString(6, m.getSubject());
			stmt.setDate(2, msgDate);
			stmt.setBoolean(3,m.isRead());
			stmt.setString(5,m.getSender());
			stmt.setString(7,m.getReceptor());
			stmt.setString(4, UIDGenerator.getInstance().getKey());
			
			stmt.executeUpdate();

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return b;

	}

	public void delete(String s) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "DELETE FROM Message WHERE (messagePOID = ?) ";
		PreparedStatement stmt = null;
	
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, s);
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.err.println("Message: " + e.getMessage());
			System.err.println("SQLState: " + e.getSQLState());
			System.err.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	}


	public List<Message> selectAllMsg(String username) {
		Connection conn = ConnectionManager.getInstance().checkOut();

		PreparedStatement stmt = null;
		List<Message> searchResults = new LinkedList<Message>();
		ResultSet result = null;
		
		
		try {

			String sql = "SELECT * FROM Message WHERE (receptorPOID = ?)";
			stmt = conn.prepareStatement(sql); // conn es una conexion valida
			
			stmt.setString(1, new JDBCUserDAO().selectUserOID(conn, username));
			stmt.executeQuery();
			result = stmt.executeQuery();

			while (result.next()) {
				Message temp = new Message();
				temp.setText(result.getString("text"));
				temp.setSubject(result.getString("subject"));
				temp.setRead(result.getBoolean("isreaded"));
				temp.setReceptor(result.getString("receptorPOID"));
				temp.setDate(result.getDate("date"));
				temp.setSender(result.getString("userPOID"));
				temp.setID(result.getString("messagePOID"));
				
				searchResults.add(temp);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}

		}
		return searchResults;
	}
	public void markAsRead(String s) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		String sql = "UPDATE Message SET isreaded=1 WHERE (messagePOID = ?) ";
		PreparedStatement stmt = null;
	
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, s);
			stmt.executeUpdate();
		} catch (SQLException e) {
			System.err.println("Message: " + e.getMessage());
			System.err.println("SQLState: " + e.getSQLState());
			System.err.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	}
}
